﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SHDQGSWEB.Models;

namespace SHDQGSWEB.Controllers
{
    public class ProductAdminController : Controller
    {
        //
        // GET: /ProductAdmin/
        ProductRepository _productRepository = new ProductRepository();


        [Authorize(Roles = "admin")]
        public ActionResult Index()
        {
            var products = _productRepository.FindAllProducts();
            return View(products);
        }

        //
        // GET: /ProductAdmin/Details/5
        [Authorize(Roles = "admin")]
        public ActionResult Details(int id)
        {
            var product = _productRepository.FindProduct(id);
            return View(product);
        }

        //
        // GET: /ProductAdmin/Create
        [Authorize(Roles = "admin")]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /ProductAdmin/Create
        [Authorize(Roles = "admin"), AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection )
        {
            
            try
            {
                var product = new Product();
                UpdateModel(product);
                _productRepository.Save();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /ProductAdmin/Edit/5
        [Authorize(Roles = "admin")]
        public ActionResult Edit(int id)
        {
            var product = _productRepository.FindProduct(id);
            return View(product);
        }

        //
        // POST: /ProductAdmin/Edit/5
        [Authorize(Roles = "admin"), AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                var product = _productRepository.FindProduct(id);
                UpdateModel(product);
                _productRepository.Save();
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /ProductAdmin/Delete/5
        [Authorize(Roles = "admin")]
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /ProductAdmin/Delete/5
        [Authorize(Roles = "admin"),AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                var product = _productRepository.FindProduct(id);
                _productRepository.Delete(product);
                _productRepository.Save();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
